package net.dfrz.oa.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import net.dfrz.oa.action.support.BaseAction;
import net.dfrz.oa.pojo.Office;
import net.dfrz.oa.service.OfficeService;
import net.dfrz.oa.utils.PageBean;

@Controller
@Scope("prototype")
@Namespace("")
@Results({
		@Result(name="office",location="office.jsp"),
		@Result(name="officesList", type="json", params={"root","jsonMap"}),
		@Result(name="officeList2", type="json", params={"root","list"})
})
public class OfficeAction extends BaseAction<Office>{

	private Office office;
	
	@Autowired
	private OfficeService officeService;
	//分页
	private PageBean<Office> pageList = new PageBean<Office>();
	Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
	private String rows;//每页显示的记录数 
    private String page;//当前第几页 
    private List<Office> officeList = null; //存放当前页数据
	//------------END
    private List<Office> list = null;
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public Office getModel() {
		office = new Office();
		return office;
	}
		
	public PageBean<Office> getPageList() {
		return pageList;
	}

	public void setPageList(PageBean<Office> pageList) {
		this.pageList = pageList;
	}

	/**
	 * 返回页面json数据，需实现get方法
	 * @return
	 */
	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	

	public List<Office> getList() {
		return list;
	}

	public void setList(List<Office> list) {
		this.list = list;
	}

	/**
	 * 办公用品库存管理初始界面
	 * @return
	 */
	@Action("office_officeManage")
	public String officeManage(){
		return "office";
	}

	/**
	 * 办公用品库存管理分页结果
	 */
	@Action("office_queryPageList")
	public String queryPageList(){
		//当前页   
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数  
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        pageList.setPageNo(intPage);
        pageList.setPageSize(number);
        
        //spring-page 分页结果
        Page<Office> page = null;
        //是否为条件查询判断
        String queryType = (String) getRequest().getParameter("type");
        if("name".equals(queryType)){//条件查找
        	String query = (String) getRequest().getParameter("officeName");
        	page = officeService.queryPageList(pageList,query);
        	//officeList = (PageDTO<TSaveoffice>) saveofficeService.queryLikeSql(pageList, query);
        }else{//无条件的初始页面
        	page = officeService.queryPageList(pageList);
        }
        officeList = page.getContent(); //当前页结果
        long count = page.getTotalElements();//总页数
        jsonMap.put("total", count);//total键 存放总记录数，必须的
        jsonMap.put("rows", officeList);//rows键 存放每页记录 list
        return "officesList";
	}
	
	/**
	 * 登记新购买的办公用品
	 * @throws IOException 
	 */
	
	@Action("office_addOffice")
	public void addOffice() throws IOException{
		//新增
		officeService.addOffice(office);
		getResponse().getWriter().write("true");
	}
	
	/**
	 * 对现有的办公用品进行添加
	 * @throws IOException 
	 */
	@Action("office_updateOffice")
	public void updateOffice() throws IOException{
		//更新办公用品
		officeService.updateOffice(office);
		getResponse().getWriter().write("true");
	}
	
	/**
	 * 查询出所有办公用品
	 */
	@Action("office_findAll")
	public String office_findAll(){
		list = officeService.getAll();
		return "officeList2";
	}
	
	/**
	 * valiateCount
	 * @throws IOException 
	 */
	@Action("office_getOfficeCountById")
	public void getOfficeCountById() throws IOException{
		Office o = officeService.getOfficeById(office.getId());
		getResponse().getWriter().write(o.getCount());
	}
	
}
